//
//  RootViewController.m
//  iHealthEHR
//
//  Created by Joseph Ayo Akinyele on 3/11/10.
//  Copyright 2010 Johns Hopkins University. All rights reserved.
//

#import "RootViewController.h"
#import "iHealthEHRAppDelegate.h"
// controllers
#import "LoginViewController.h"
#import "UserSettings.h"
@class LoginViewController;

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@implementation RootViewController
@synthesize controllers;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	self.title = @"iHealth Login";
	NSMutableArray *array = [[NSMutableArray alloc] init];
	// allocate login view controller
	LoginViewController *con1 = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];	
	con1.title = @"Google Health";
	con1.view.backgroundColor = UIColorFromRGB(0x5993E5);
	con1.GoogleHealthLogin = YES;
	
	LoginViewController *con2 = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];	
	con2.title = [UserSettings getActiveHospitalName];
	con2.view.backgroundColor = UIColorFromRGB(0x778899);
	con2.JHUHospitalLogin = YES;
	
	// add controller to list of controllers
	[array addObject: con2];
	[array addObject: con1];	
	
	// local access view controller
	if([UserSettings getCacheSetting]) {
		LoginViewController *con3 = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
		con3.title = @"Offline Access";
		con3.view.backgroundColor = UIColorFromRGB(0x77A1B2);
		con3.OfflineAccessLogin = YES;
		[array addObject: con3];
	}
	
	self.controllers = array;
	[array release];	
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
	// [self.navigationController setToolbarHidden:YES];	
	[super viewWillAppear:animated];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[controllers release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods

// Used to ask how many rows are in a particular section.
-(NSInteger) tableView:(UITableView *) tableView 
 numberOfRowsInSection:(NSInteger) section
{
	return [self.controllers count];
}

// 
-(UITableViewCell *) tableView:(UITableView *) tableView
		 cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
	static NSString *RootViewControllerCell = @"RootViewControllerCell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: RootViewControllerCell];
	if(cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: RootViewControllerCell] autorelease];
	}
	
	// Configure the cell
	NSUInteger row = [indexPath row];
	UIViewController *controller = [controllers objectAtIndex:row];
	[cell.textLabel setText:controller.title];
	// cell.image = controller.rowImage;
	return cell;
}

// Check item that is about to get selected. Effectively you dont allow user to select
// if index selected is on 0.
-(NSIndexPath *) tableView: (UITableView *) tableView
  willSelectRowAtIndexPath: (NSIndexPath *)indexPath
{	
	return indexPath;
}

// didSelectRowAtIndexPath gets called when user selects a specific cell in the table.
/*-(void) tableView: (UITableView *) tableView
 didSelectRowAtIndexPath: (NSIndexPath *) indexPath
 {
 NSUInteger row = [indexPath row];
 NSString *rowValue = [controllers objectAtIndex:row];
 
 NSString *message = [[NSString alloc] initWithFormat:@"You selected '%@'", rowValue];
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected!" 
 message:message 
 delegate:nil 
 cancelButtonTitle:@"Yes I Did!" 
 otherButtonTitles:nil];
 [alert show];
 [message release];
 [alert release]; 
} */

#pragma mark -
#pragma mark Table View Delegate Methods
/*
 -(UITableViewCellAccessoryType) tableView:(UITableView *)tableView
 accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellAccessoryDisclosureIndicator;
} */


// Example of how to do drill down
-(void) tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	// get the row from indexPath
	NSUInteger row = [indexPath row];
	// grab the correct contoller from our array that corresponds to that row
	UIViewController *nextController = [self.controllers objectAtIndex:row];
	
	// Since the nav controller is maintained by the app, use shared UIApp instance
	// to grab a reference to that delegate
	iHealthEHRAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	// use nav outlet to push the next controller, the one we pulled from our array onto 
	// the nav controller stack.
	[delegate.navController pushViewController:nextController animated:YES];	
}

@end
